/*
package com.deer.wms.base.system.web.rocketmq;

import com.alibaba.fastjson.JSONArray;
import com.deer.wms.base.system.finalUtil.*;
import com.deer.wms.base.system.model.Rocket;
import com.deer.wms.base.system.model.TaskStock;
import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.box.*;
import com.deer.wms.base.system.model.item.ItemInfo;
import com.deer.wms.base.system.model.task.PickTaskDto;
import com.deer.wms.base.system.model.task.TaskInfo;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.model.ware.CellInfo;
import com.deer.wms.base.system.service.bill.IBillInDetailService;
import com.deer.wms.base.system.service.bill.IBillInMasterService;
import com.deer.wms.base.system.service.bill.IBillOutDetailService;
import com.deer.wms.base.system.service.bill.IBillOutMasterService;
import com.deer.wms.base.system.service.box.*;
import com.deer.wms.base.system.service.item.IItemInfoService;
import com.deer.wms.base.system.service.rocket.RocketService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.base.system.service.task.PickTaskService;
import com.deer.wms.base.system.service.task.TaskInfoHistoryService;
import com.deer.wms.base.system.service.ware.ICellInfoService;
import com.deer.wms.base.system.util.JedisServiceUtil;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.result.CommonCode;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.utils.StringUtils;
import com.deer.wms.framework.util.ShiroUtils;
import com.deer.wms.system.domain.SysUser;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

import static java.util.Comparator.comparing;

@RestController
@RequestMapping("/rocketmq")
@Slf4j
public class RocketmqController extends BaseController {
    @Autowired
    private Producer producer;
    @Autowired
    private PickTaskService pickTaskService;
    @Autowired
    private ITaskInfoService taskInfoService;//任务处理层

    @Autowired
    private TaskInfoHistoryService taskInfoHistoryService;

    @Autowired
    private IBillInDetailService billInDetailService;//入库单明细

    @Autowired
    private IBillInMasterService billInMasterService;

    @Autowired
    private RocketService rocketService;//记录消息

    @Autowired
    private IBoxItemService boxItemService;//组盘

    @Autowired
    private IBillOutDetailService billOutDetailService;//出库单明细

    @Autowired
    private IBillOutMasterService billOutMasterService;

    @Autowired
    private IItemInfoService itemInfoService;

    @Autowired
    private BoxInfoService boxInfoService;
    @Autowired
    private ICellInfoService cellInfoService;
    @Autowired
    private AreaBoxItemService areaBoxItemService;
    @Autowired
    private AreaBoxItemHistoryService areaBoxItemHistoryService;
    @Autowired
    private BoxItemBacthService boxItemBacthService;
    @Autowired
    private JedisServiceUtil jedisServiceUtil;

    */
/**
     * web端入库作业
     *
     * @param inserData
     * @return
     * @throws ParseException
     *//*

    @RequestMapping(value = "/push", method = RequestMethod.POST)
    @Transactional
    @ResponseBody
    public Result billInPushMsg(@RequestBody InserData inserData) throws Exception {
        TaskInfo taskInfo = new TaskInfo();//任务对象
        BoxInfo boxInfo = new BoxInfo();
        BillInMaster billInMaster = inserData.getBillInMaster();
        billInMaster.setState(BillState.BILLIN_STATE_THREE);//正在入库
        billInMasterService.updateBillInMaster(billInMaster);
        //同步任务表
        for (BillInDetail billInDetail : inserData.getBillInDetailList()) {
            String createUserName = ShiroUtils.getLoginName();
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            taskInfo.setPid(sysUser.getPid());//添加班次
            taskInfo.setCreateUserName(createUserName);
            long taskNumber = taskInfoService.generateTaskNumber();//设置任务号
            taskInfo.setTaskNumber(taskNumber);
            taskInfo.setType(TaskInfoState.BILLIN_TYPE);//任务类型
            taskInfo.setState(TaskInfoState.STATE_ZERO);//状态 0已下发
            taskInfo.setBillId(billInDetail.getBillId());
            taskInfo.setRoadWay(billInDetail.getRoadWay());
            taskInfo.setQuantity(billInDetail.getSurplusReceivedQuantity());//托盘上放的数量
            taskInfo.setBoxCode(billInDetail.getBoxCode());//托盘编号
            taskInfo.setBillInDetailId(billInDetail.getBillInDetailId());//明细ID
            taskInfo.setStowCreateTime(billInDetail.getStowCreateTime());//入库时，托盘码垛的时间
            //更新已入库数量
               */
/* billInDetail.setQuantityReceived(billInDetail.getQuantityReceived() + billInDetail.getSurplusReceivedQuantity());
                billInDetailService.updateBillInInfo(billInDetail);*//*

            // 分配货位
               */
/* boolean flag = taskInfoService.setTaskInfoCellInfo(taskInfo, billInDetail.getItemId(),billInDetail.getRoadWay());
                if (flag){
                    Integer cellId = taskInfo.getCellId();
                    CellInfo info = cellInfoService.selectCellInfoById(cellId);
                    info.setState(3);// 设置为状态3，说明此货位已经分配了入库任务
                    cellInfoService.updateCellInfo(info);//更新货位状态为入库中
                }*//*

            taskInfoService.insertBillInTaskInfo(taskInfo);//同步信息到任务表，
            //更新托盘状态
            boxInfo.setBoxCode(billInDetail.getBoxCode());
            boxInfo.setBoxState(BoxInfoState.BOXINFO_STATE_TWO);
            boxInfo.setHasGoods(BoxInfoState.BOXINFO_GOODS_ONT);
            boxInfoService.saveOrUpdate(boxInfo);
        }
        return ResultGenerator.genSuccessResult();
    }

    */
/**
     * 手持机入库作业
     *
     * @param inserData
     * @return
     * @throws ParseException
     *//*

    @RequestMapping(value = "/billInPush", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public Result billInPush(@RequestBody InserData inserData) throws Exception {
        // 新增对条码的校验，如果库存中已经存在此条码的托盘,则返回失败
        TaskStock taskStock = inserData.getTaskStock();
        if (null != taskStock && StringUtils.isNotEmpty(taskStock.getBoxCode())){
            String boxCode = taskStock.getBoxCode();


            // 校验此托盘是否存在出库任务未完成但是已经托盘实际上已经出库的异常情况
            TaskInfo info = taskInfoService.selectHaveOldTaskInfoByBoxCode(boxCode);
            BoxItemDto boxItemDto = boxItemService.selectBoxItemByBoxCode(boxCode);
            BoxInfo boxInfoByBoxCode = boxInfoService.getBoxInfoByBoxCode(boxCode);
            if (null != boxItemDto || null != info){
                return ResultGenerator.genFailResult(CommonCode.BXOINFO_ERROR);
            }
            // 如果存在此托盘,且此托盘的状态不为空,则说明此托盘正在使用,返回托盘正在使用的错误信息s
            if (null !=boxInfoByBoxCode && !boxInfoByBoxCode.getBoxState().equals(BoxInfoState.BOXINFO_GOODS_ZERO)){
                return ResultGenerator.genFailResult(CommonCode.BXOINFO_ERROR);
            }


        }
        InserData insertResult = insert(inserData);
        try {
            TaskInfo taskInfo = new TaskInfo();//任务对象
            BoxInfo boxInfo = new BoxInfo();
            BillInMaster billInMaster = insertResult.getBillInMaster();
            billInMaster.setState(BillState.BILLIN_STATE_THREE);//正在入库
            billInMasterService.updateBillInMaster(billInMaster);
            //同步任务表
            for (BillInDetail billInDetail : insertResult.getBillInDetailList()) {
                String createUserName = ShiroUtils.getLoginName();
                SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
                taskInfo.setPid(sysUser.getPid());//添加班次
                taskInfo.setCreateUserName(createUserName);
                long taskNumber = taskInfoService.generateTaskNumber();//设置任务号
                taskInfo.setTaskNumber(taskNumber);
                taskInfo.setType(TaskInfoState.BILLIN_TYPE);//任务类型
                taskInfo.setState(TaskInfoState.STATE_ZERO);//状态
                taskInfo.setRoadWay(billInDetail.getRoadWay());
                taskInfo.setQuantity(billInDetail.getQuantity());//托盘上放的数量
                taskInfo.setBoxCode(billInDetail.getBoxCode());//托盘编号
                taskInfo.setBillInDetailId(billInDetail.getBillInDetailId());//明细ID
                taskInfo.setBillId(billInDetail.getBillId());//入库单ID
                taskInfo.setStowCreateTime(billInDetail.getStowCreateTime());//入库时，托盘码垛的时间
                //更新已入库数量
//            billInDetail.setQuantityReceived(billInDetail.getQuantityReceived() + billInDetail.getSurplusReceivedQuantity());
//            billInDetailService.updateBillInInfo(billInDetail);
                // 分配货位
              */
/*  boolean flag = taskInfoService.setTaskInfoCellInfo(taskInfo, billInDetail.getItemId(), billInDetail.getRoadWay());
                if (flag) {
                    Integer cellId = taskInfo.getCellId();
                    CellInfo info = cellInfoService.selectCellInfoById(cellId);
                    info.setState(3);// 设置为状态3，说明此货位已经分配了入库任务
                    cellInfoService.updateCellInfo(info);//更新货位状态为入库中
                }*//*

                taskInfoService.insertBillInTaskInfo(taskInfo);//同步信息到任务表，任务表与队列消息一致
                //更新托盘状态
                boxInfo.setBoxCode(billInDetail.getBoxCode());
                boxInfo.setBoxState(BoxInfoState.BOXINFO_STATE_TWO);//任务中
                boxInfo.setHasGoods(BoxInfoState.BOXINFO_GOODS_ONT);//有货
                boxInfoService.saveOrUpdate(boxInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, e.getMessage());
        }
        return ResultGenerator.genSuccessResult();
    }

    */
/**
     * 手持机添加入库信息
     *//*

    @Transactional(rollbackFor = Exception.class)
    public InserData insert(InserData inserData) throws ParseException {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String createUserName = ShiroUtils.getLoginName();
        Integer userId = ShiroUtils.getUserId();
        BillInMaster billInMaster = inserData.getBillInMaster();
        String billNo = billInMaster.getBillNo();
        billInMaster.setCreateUserName(createUserName);
        billInMaster.setCreateUserId(userId);
        billInMaster.setPid(sysUser.getPid());//添加班次
        billInMaster.setState(BillState.BILLIN_STATE_TWO);//审核通过
//        billInMaster.setCreateTime(new Date().toString());
        //当点击继续作业时,判断单据号是否存在,如存在则不再次新增基础数据
        Boolean aboolean = false;
        BillInMaster iBillInMaster = null;
        // 新增billInMaster,相当于新增入库单
        if (billInMasterService.selectBillInMasterByBillNo(billNo) == null) {
            aboolean = billInMasterService.createInfoByBillNO(billInMaster);
            int billId = billInMaster.getBillId();
        } else {
            iBillInMaster = billInMasterService.selectBillInMasterByBillNo(billNo);
        }
//        BillInMaster iBillInMaster = billInMasterService.selectBillInMasterById(billInMaster.getBillId());
//        String ibillNo = iBillInMaster.getBillNo();
//        if( !billNo.equals(ibillNo) ){
//            billInMasterService.createInfoByBillNO(billInMaster);
//        }
        // 获取手持机传入的入库明细信息
        List<BillInDetail> billInDetailList = inserData.getBillInDetailList();
        Boolean aBoolean1 = false;
        for (BillInDetail billInDetail : billInDetailList) {
            // 关联billInMaster和billInDetail
            if (aboolean) {
                billInDetail.setBillId(billInMaster.getBillId());
            } else {
                billInDetail.setBillId(iBillInMaster.getBillId());
            }
            //为billInDetail表设置班次信息
            billInDetail.setPid(sysUser.getPid());
            //设置生产日期pdTime
            String batch = billInDetail.getBatch();
            String[] batchStrs = batch.split("/");
            String pdTime = batchStrs[0] + "-" + batchStrs[1] + "-" + batchStrs[2];
            billInDetail.setPdTime(pdTime);
            // 保存billInDetail
            aBoolean1 = billInDetailService.saveBillInDetail(billInDetail);
            //增加批次
            BoxItemBacth byBatchName = boxItemBacthService.findByBatchName(batch);
            if (null==byBatchName){
                BoxItemBacth boxItemBacth = new BoxItemBacth();
                boxItemBacth.setBatchName(batch);
                boxItemBacthService.saveBoxItemBacth(boxItemBacth);
            }
        }
        if (aBoolean1) {
            inserData.setSaveResult(true);
            return inserData;
        }
        inserData.setSaveResult(false);
        return inserData;
    }

    */
/**
     * Web端出库放入队列
     *
     * @param inserData
     * @return
     * @throws ParseException
     *//*

    @RequestMapping(value = "/billOutPush", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result billOutPushMsg(@RequestBody InserData inserData) throws Exception {
        BillOutMaster billOutMaster = inserData.getBillOutMaster();
        if (billOutMaster.getAreaId()==259){
            //暂存区出库
            return this.areaBoxitemBillOut(inserData);
        }else if (billOutMaster.getAreaId()==248){
          */
/*  //立库区出库
            List<BillOutDetail> billOutDetailList = inserData.getBillOutDetailList();
            for (BillOutDetail billOutDetail : billOutDetailList) {
                if (null != billOutDetail.getBoxCodeList()) {
                    return this.assignBillOutPushMsg(inserData);//指定货位出库出库
                } else {*//*

                    return this.BillOut(inserData);//先进先出、指定批次、指定日期出库
                //}
            }


        return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
    }


    */
/**
     * 手持机出库放入队列
     *
     * @param inserData
     * @return
     * @throws ParseException
     *//*

    @RequestMapping(value = "/smartBillOutPushMsg", method = RequestMethod.POST)
    @ResponseBody
    public Result smartBillOutPushMsg(@RequestBody InserData inserData) throws Exception {
        boolean saveResult = this.save(inserData);
        if (saveResult) {
            BillOutMaster billOutMaster = inserData.getBillOutMaster();
            if (billOutMaster.getAreaId()==259){
                //暂存区出库
                return this.areaBoxitemBillOut(inserData);
            }else if (billOutMaster.getAreaId()==248){
                //立库区出库
                return this.BillOut(inserData);
            }

        }
        return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
    }

    */
/**
     * 暂存区出库
     * @param inserData
     * @return
     * @throws Exception
     *//*


    @Transactional(rollbackFor = Exception.class)
    public Result areaBoxitemBillOut(InserData inserData) throws Exception {
        List<BillOutDetail> billOutDetailList = inserData.getBillOutDetailList();
        for (BillOutDetail billOutDetail : billOutDetailList) {
            String itemCode = billOutDetail.getItemCode();//暂存区出库的物料
            Integer quantity = billOutDetail.getQuantity();//出货的数量
            BillOutMaster billOutMaster = inserData.getBillOutMaster();
            billOutMaster.setState(3);//任务已完成的状态
            int editBillOutRe=billOutMasterService.updateBillOutMaster(billOutMaster);
            billOutDetail.setQuantityDelivery(quantity);//出库实际数量
            int editBillOutDeRe = billOutDetailService.updateBillOutMasterQuantityDelivery(billOutDetail);//更新出库明细
            //同步任务表
            TaskInfo taskInfo = new TaskInfo();//任务对象
            String createUserName = ShiroUtils.getLoginName();
            taskInfo.setCreateUserName(createUserName);
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            taskInfo.setPid(sysUser.getPid());//添加班次
            taskInfo.setType(TaskInfoState.BILLOUT_TYPE);//任务类型
            taskInfo.setState(TaskInfoState.STATE_ZERO);//状态
            taskInfo.setBillId(billOutMaster.getBillId());
            taskInfo.setBillOutDetailId(billOutDetail.getBillOutDetailId());//明细ID
            long taskNumber = taskInfoService.generateTaskNumber();//设置任务号
            taskInfo.setTaskNumber(taskNumber);
            taskInfo.setQuantity(quantity);//数量
            int saveTaskRe = taskInfoService.insertTaskInfo(taskInfo);
            if(editBillOutRe==0||editBillOutDeRe==0||saveTaskRe==0){
                return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
            }
            //更新暂存区库存
            AreaBoxItemDto re = new AreaBoxItemDto();
            re.setItemCode(itemCode);
            re.setBatch(billOutDetail.getBatch());
            List<AreaBoxItemDto> areaBoxItemList  = areaBoxItemService.findAreaBoxItemList(re);
             for (AreaBoxItemDto boxItemDto : areaBoxItemList) {
                    Integer quantityReceived = quantity;//出库数量
                    if(quantityReceived==boxItemDto.getQuantity()){
                        //同步暂存区
                        areaBoxItemHistoryService.saveAreaBoxItem(boxItemDto);
                        areaBoxItemService.delectAreaBoxItembyId(boxItemDto.getId());
                        break;
                    }else if(quantityReceived<boxItemDto.getQuantity()){
                        AreaBoxItem areaBoxItem = new AreaBoxItem();
                        areaBoxItem.setId(boxItemDto.getId());
                        areaBoxItem.setQuantity(boxItemDto.getQuantity()-quantityReceived);
                        areaBoxItemService.editAreaBoxItem(areaBoxItem);//修改暂存区物料的数量
                        boxItemDto.setQuantity(quantityReceived);
                        areaBoxItemHistoryService.saveAreaBoxItem(boxItemDto);//将出库的暂存区物料数量添加到暂存区历史
                        break;
                    }else {
                        areaBoxItemHistoryService.saveAreaBoxItem(boxItemDto);
                        areaBoxItemService.delectAreaBoxItembyId(boxItemDto.getId());
                    }
            }
            int saveTaskHistRe = taskInfoHistoryService.saveTaskInfoHistory(taskInfoService.selectTaskInfoById(taskInfo.getId()));//任务完成同步到历史表
            int deTaskRe= taskInfoService.deleteTaskInfoById(taskInfo.getId());//删除已完成的任务表
            //出库报表
            PickTaskDto pickTask = new PickTaskDto();
            */
/* pickTask.setCreateTime(billOutDetailDto.getCreateTime());//出库时间*//*

            pickTask.setBillOutDetailId(billOutDetail.getBillOutDetailId());
            //pickTask.setWareId(billOutDetail.getWareId());//仓库
            pickTask.setPickQuantity(billOutDetail.getQuantity());//出库数量
            pickTask.setPickState(4);//1-待下发，2-已下发任务,3-点数任务已完成，4-库存已扣减，5-取消
            pickTask.setBatch(billOutDetail.getBatch()); //批次
            pickTask.setBoxCode(billOutDetail.getBoxCode());//箱号
            //pickTask.setStowCreateTime(billOutDetail.getStowCreateTime());
            int savePickRe = pickTaskService.savePickTask(pickTask);
            if(saveTaskHistRe==0||deTaskRe==0||savePickRe==0){
                try {
                    log.error("暂存区出库失败");
                    throw new Exception("暂存区出库失败，手动抛异常");
                } catch (Exception e) {
                    e.printStackTrace();
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
                return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
            }

        }
        return ResultGenerator.genSuccessResult();
    }


    */
/**
     * 手持机增加出库单
     * @param inserData
     * @return
     *//*

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/insert")
    public boolean save(InserData inserData) {
        String createUserName = ShiroUtils.getLoginName();
        SysUser sysUser = (SysUser)SecurityUtils.getSubject().getPrincipal();
        Integer userId = ShiroUtils.getUserId();
        BillOutMaster billOutMaster = inserData.getBillOutMaster();
        billOutMaster.setCreateUserName(createUserName);
        billOutMaster.setCreateUserId(userId.intValue());
        billOutMaster.setState(0);
        billOutMaster.setType(1);
        billOutMaster.setWareId(1220);
        billOutMaster.setPid(sysUser.getPid());//添加班次
        Boolean aBoolean1 = billOutMasterService.saveBillOutMaster(billOutMaster);
        Boolean aBoolean = false;
        List<BillOutDetail> billOutDetailList = inserData.getBillOutDetailList();
        for (BillOutDetail nbillOutDetail : billOutDetailList) {
            nbillOutDetail.setBillId(billOutMaster.getBillId());
            ItemInfo itemInfo = itemInfoService.findByItemCode(nbillOutDetail.getItemCode());
            nbillOutDetail.setItemSpecificationId(itemInfo.getItemSpecificationId());
//            nbillOutDetail.setQuantitySum(nbillOutDetail.getQuantitySum()-nbillOutDetail.getQuantity());
            aBoolean = billOutDetailService.saveBillOutDetail(nbillOutDetail);
        }
        if (aBoolean && aBoolean1) {
            return true;
        }
        return false;
    }

    */
/***
     *出库发送消息的方法
     * @author 李帅辰
     * @date 2020/10/4 16:39
     * @return void
     *//*

    public void outMq(BoxItemDto BoxItemOut, List<TaskInfo> taskInfoList, String topic) {
        String body = JSONArray.toJSONString(BoxItemOut);
        SendResult stations = null;//放入队列,返回消息状态
        try {
            stations = producer.send(topic, "pull", body);
            TaskInfo taskInfo1 = new TaskInfo();
            //同步rocket表
            Rocket rocket = new Rocket();
            rocket.setMessageId(stations.getMsgId());
            rocket.setBody(body);
            rocketService.saveRocket(rocket);

            for (TaskInfo taskInfo : taskInfoList) {
                if(BoxItemOut.getBoxCode().equals(taskInfo.getBoxCode())){
                    //任务表关联 消息rocket表
                    taskInfo1.setId(taskInfo.getId());
                    taskInfo1.setMessageId(rocket.getId());
                    taskInfoService.updateTaskInfo(taskInfo1);
                }
            }
           // this.editTaskInfo(stations, taskInfoList, body);//消息表和任务表同步
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


*/
/*    *//*
*/
/**
     * 消息表和任务表同步
     *
     * @param stations
     * @param taskInfoIdList
     * @param body
     *//*
*/
/*
    @Transactional
    public void editTaskInfo(SendResult stations, List<TaskInfo> taskInfoIdList, String body) {
        //消息成功后操作
        //List<String> taskInfoIdlist = Arrays.asList(taskInfoId.split(","));
        TaskInfo taskInfo1 = new TaskInfo();
        //同步rocket表
        Rocket rocket = new Rocket();
        rocket.setMessageId(stations.getMsgId());
        rocket.setBody(body);
        rocketService.saveRocket(rocket);

        for (TaskInfo taskInfo : taskInfoIdList) {
            //任务表关联 消息rocket表
            taskInfo1.setId(taskInfo.getId());
            taskInfo1.setMessageId(rocket.getId());
            taskInfoService.updateTaskInfo(taskInfo1);
        }
    }*//*




    */
/**
     * 出库
     * @param inserData
     * @return
     *//*

    @Transactional(rollbackFor = Exception.class)
    public Result BillOut(InserData inserData){
        try {
            Integer sumQuantity = 0;//初始化总数
            List<BoxItemDto> BoxItemOutList = new ArrayList<>();//队列放入的托盘详细信息集合
            List<TaskInfo> taskInfoList = new ArrayList<>();//对列放入的任务集合
            //判断库存
            for (BillOutDetail billOutDetail : inserData.getBillOutDetailList()) {
                if(billOutDetail.getQuantity()<=0){
                    return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
                }
                    BoxItem boxItem = new BoxItem();
                    if (null != billOutDetail.getItemCode()) {
                        boxItem.setItemCode(billOutDetail.getItemCode());
                    }
                    if (null != billOutDetail.getBatch()) {
                        boxItem.setBatch(billOutDetail.getBatch());
                    }
                    if (null != billOutDetail.getStartTime() && null != billOutDetail.getEndTime()) {
                        boxItem.setStartTime(billOutDetail.getStartTime());
                        boxItem.setEndTime(billOutDetail.getEndTime());
                    }
                    Integer sumQuantityByItemCode = boxItemService.getSumQuantityByItemCode(boxItem);//获取物料的总库存数
                    if(null==sumQuantityByItemCode){
                        return ResultGenerator.genFailResult(CommonCode.STOCK_NOT_ENOUGH, "false");
                    }
                    //判断不够出库就返回false
                    if (sumQuantityByItemCode < billOutDetail.getQuantity()) {
                        return ResultGenerator.genFailResult(CommonCode.STOCK_NOT_ENOUGH, "false");
                    }
            }
            //开始出库
            for (BillOutDetail billOutDetail : inserData.getBillOutDetailList()) {
                if (null != billOutDetail.getBoxCodeList()) {
                    return this.assignBillOutPushMsg(inserData);//指定货位出库出库
                }
                String itemCode = billOutDetail.getItemCode();//出库的单个物料
                Integer quantity = billOutDetail.getQuantity();//出库单个物料的总数量
                BoxItemDto brequest = new BoxItemDto();
                brequest.setItemCode(itemCode);
                if (null != billOutDetail.getBatch()) {
                    brequest.setBatch(billOutDetail.getBatch());
                }//指定批次
                if (null != billOutDetail.getStartTime() && null != billOutDetail.getEndTime()) {
                    brequest.setStartTime(billOutDetail.getStartTime());
                    brequest.setEndTime(billOutDetail.getEndTime());
                }//指定时间
                List<BoxItemDto> listByItemCode = boxItemService.findListByItemCode(brequest);//获得库存

                if (listByItemCode.size() > 0) {
                    for (BoxItemDto boxItemDto : listByItemCode) {
                        //判断是否是需盘点
                        if (boxItemDto.getTeamPai() % 2 != 0) {
                            Integer cellId = boxItemDto.getCellId();
                            Integer sPai = boxItemDto.getsPai();//排
                            Integer sLie = boxItemDto.getsLie();//列
                            Integer sFloor = boxItemDto.getsFloor();//层
                            Integer teamPai = boxItemDto.getTeamPai();//分划排
                            if(sPai% 2 != 0){
                                sPai=sPai+1;
                            }else{
                                sPai=sPai-1;
                            }
                            teamPai=teamPai+1;

                            CellInfo cellInfo = new CellInfo();
                            cellInfo.setsPai(sPai);
                            cellInfo.setsLie(sLie);
                            cellInfo.setsFloor(sFloor);
                            cellInfo.setTeamPai(teamPai);
                            CellInfo stateByCellInfo = cellInfoService.findStateByCellInfo(cellInfo);
                            if (stateByCellInfo.getState() == 6) {
                               continue;
                           }
                        }
                        TaskInfo taskInfo = new TaskInfo();//任务对象
                        sumQuantity = boxItemDto.getQuantity() + sumQuantity;//当前托盘上物品的数量，累加，拿出足够出库数量的托盘   计算
                        //更新出库单状态
                        BillOutMaster billOutMaster = inserData.getBillOutMaster();
                        billOutMaster.setState(2);//任务已下发的状态
                        int editBillOut=billOutMasterService.updateBillOutMaster(billOutMaster);
                        //同步任务表
                        String createUserName = ShiroUtils.getLoginName();
                        taskInfo.setCreateUserName(createUserName);
                        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
                        taskInfo.setPid(sysUser.getPid());//添加班次
                        taskInfo.setType(TaskInfoState.BILLOUT_TYPE);//任务类型
                        taskInfo.setState(TaskInfoState.STATE_ZERO);//状态
                        taskInfo.setBillId(billOutMaster.getBillId());
                        taskInfo.setBoxCode(boxItemDto.getBoxCode());//托盘编号
                        taskInfo.setBillOutDetailId(billOutDetail.getBillOutDetailId());//明细ID
                        long taskNumber = taskInfoService.generateTaskNumber();//设置任务号
                        taskInfo.setTaskNumber(taskNumber);
                        taskInfo.setQuantity(boxItemDto.getQuantity());//数量
                        taskInfo.setRoadWay(boxItemDto.getShelfId());//巷道
                        taskInfo.setCellCode(boxItemDto.getCellCode());//货位编码
                        taskInfo.setCellId(boxItemDto.getCellId());//货位ID
                        taskInfo.setStowCreateTime(boxItemDto.getStowCreateTime());//入库时，托盘码垛的时间
                        int saveTask=taskInfoService.insertTaskInfo(taskInfo);//同步信息到任务表，任务表与队列消息一致
                        taskInfoList.add(taskInfo);
                        BoxInfo boxInfo = new BoxInfo();
                        boxInfo.setBoxCode(boxItemDto.getBoxCode());
                        boxInfo.setBoxState(BoxInfoState.BOXINFO_STATE_TWO);
                        boxInfo.setHasGoods(BoxInfoState.BOXINFO_GOODS_ONT);
                        int editBoxInfo=boxInfoService.updateBoxInfo(boxInfo);
                        CellInfo cellInfo = new CellInfo();
                        cellInfo.setState(CellInfoState.STATE_TWO);//出库中的状态
                        cellInfo.setCellId(boxItemDto.getCellId());
                        int editCell=cellInfoService.updateCellInfo(cellInfo);
                        if(editBillOut==0||saveTask==0||editBoxInfo==0||editCell==0){
                            try {
                                log.error("修改出库单状态："+editBillOut+"下发任务状态："+saveTask+"修改托盘状态："+editBoxInfo+"修改货位状态："+editCell);
                                throw new Exception("出库下发任务出错，手动抛异常");
                            } catch (Exception e) {
                                e.printStackTrace();
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            }
                            if(editBillOut==0){
                                return ResultGenerator.genFailResult(CommonCode.EDITBILLOUT_ERROR, "false");
                            }else if(saveTask==0){
                                return ResultGenerator.genFailResult(CommonCode.TASKINFO_OUTERROR, "false");
                            }else if(editBoxInfo==0){
                                return ResultGenerator.genFailResult(CommonCode.EDITBOXINFO_ERROR, "false");
                            }else if(editCell==0){
                                return ResultGenerator.genFailResult(CommonCode.EDITCELLINFO_ERROR, "false");
                            }

                        }
                        BoxItemDto boxItemDtoOut = new BoxItemDto();
                        boxItemDtoOut.setBoxCode(boxItemDto.getBoxCode());
                        boxItemDtoOut.setBillOutDetailId(billOutDetail.getBillOutDetailId());
                        BoxItemDto boxItemDto1 = boxItemService.selectBoxItemByboxItemDto(boxItemDtoOut);
                        if(null == boxItemDto1){
                            logger.error("托盘号："+boxItemDto.getBoxCode()+",发送消息时box_item表信息错误，没有下发成功");
                        }
                        BoxItemOutList.add(boxItemDto1);//放入要发送的消息内容
                        if (sumQuantity >= quantity) {
                            Integer billOutDetailId = billOutDetail.getBillOutDetailId();
                            BillOutDetail bybillOutDetailId = billOutDetailService.findBybillOutDetailId(billOutDetailId);
                            billOutDetail.setQuantityDelivery(sumQuantity+bybillOutDetailId.getQuantityDelivery());//出库实际数量
                            billOutDetail.setSendBack(sumQuantity - quantity+bybillOutDetailId.getSendBack());//比实际多余的数量
                            //将多余的料分配进暂存区
                            if (sumQuantity > quantity) {
                                AreaBoxItem areaBoxItem = new AreaBoxItem();
                                areaBoxItem.setItemCode(boxItemDto.getItemCode());
                                areaBoxItem.setBatch(boxItemDto.getBatch());
                                areaBoxItem.setQuantity(sumQuantity - quantity);
                                areaBoxItem.setBillOutDtailId(billOutDetail.getBillOutDetailId());
                                areaBoxItem.setPd(boxItemDto.getPd());
                                areaBoxItem.setExp(boxItemDto.getExp());
                                areaBoxItemService.saveAreaBoxItem(areaBoxItem);
                            }
                            // areaBoxItemHistoryService.saveAreaBoxItem(areaBoxItem);
                            billOutDetailService.updateBillOutMasterQuantityDelivery(billOutDetail);//更新出库明细
                            break;
                        }
                    }

                    //拿出货位信息，判断是否是需盘点；是，能continue;   如果当前托盘在组内的奇数排，偶数排的货需盘点，则continue
                    //分 奇偶 两组  偶数优先
                    //按照组内的2413，先高再低的原则，分配已经找到满足条件的托盘顺序
                    //BoxItemOutList.sort(comparing(BoxItemDto::getTeamPai));
                    if (BoxItemOutList.size() == 0) {
                        return ResultGenerator.genFailResult(CommonCode.STOCK_NOT_ENOUGH, "false");
                    }
                    List<Integer> collegeOrder = Arrays.asList(2, 4, 3, 1);//对排进行排序
                    List<Integer> addressOrder = Arrays.asList(4, 3, 2, 1);//对层进行排序
                    Collections.sort(BoxItemOutList, new Comparator<BoxItemDto>() {
                        public int compare(BoxItemDto boxItemDto1, BoxItemDto boxItemDto2) {
                            if (boxItemDto1.getTeamPai().equals(boxItemDto2.getTeamPai())) {
                                int io1 = addressOrder.indexOf(boxItemDto1.getsFloor());
                                int io2 = addressOrder.indexOf(boxItemDto2.getsFloor());
                                return io1 - io2;
                            } else {
                                int io1 = collegeOrder.indexOf(boxItemDto1.getTeamPai());
                                int io2 = collegeOrder.indexOf(boxItemDto2.getTeamPai());
                                return io1 - io2;
                            }
                        }
                    });
                    //排序结束
                    for (BoxItemDto boxItemDto1 : BoxItemOutList) {
                        Integer roadWay = boxItemDto1.getRoadWay();
                        //boxCodeList.add(boxItemDto1);
                        String topic = null;
                        if (roadWay == 76) {
                            topic = TopicName.TOPIC_ONE;
                            outMq(boxItemDto1, taskInfoList, topic);
                        } else if (roadWay == 77) {
                            topic = TopicName.TOPIC_TWO;
                            outMq(boxItemDto1, taskInfoList, topic);
                        } else if (roadWay == 79) {
                            topic = TopicName.TOPIC_THREE;
                            outMq(boxItemDto1, taskInfoList, topic);
                        }
                    }

                }else {
                    return ResultGenerator.genFailResult(CommonCode.STOCK_NOT_ENOUGH, "false");
                }
            }
            return ResultGenerator.genSuccessResult();

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
        }
    }






    */
/***
     *指定货位出库
     * @author 李帅辰
     * @date 2020/10/4 18:15 s
     * @return com.deer.wms.common.core.result.Result
     *//*

    @RequestMapping(value = "/assignBillOutPushMsg", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public Result assignBillOutPushMsg(InserData inserData) throws Exception {
        try {
            Integer sumQuantity = 0;//初始化总数
            List<BoxItemDto> BoxItemOutList = new ArrayList<>();//队列放入的托盘详细信息集合1.0
            List<TaskInfo> taskInfoList = new ArrayList<>();//对列放入的任务集合

            //开始出库
            for (BillOutDetail billOutDetail : inserData.getBillOutDetailList()) {
                List<BoxItemDto> BoxCodeList = new ArrayList<>();
                //遍历指定出库的托盘，查询得到需要的数据
                for (String boxCode : billOutDetail.getBoxCodeList()) {
                    BoxItemDto brequest = new BoxItemDto();
                    brequest.setBoxCode(boxCode);
                    BoxItemDto assignFindBoxCode = boxItemService.assignBillOutFindBoxCode(brequest);
                    BoxCodeList.add(assignFindBoxCode);
                }
                if (BoxCodeList.size() > 0) {
                    for (BoxItemDto boxItemDto : BoxCodeList) {
                        TaskInfo taskInfo = new TaskInfo();//任务对象
                        sumQuantity = boxItemDto.getQuantity() + sumQuantity;//当前托盘上物品的数量，累加，拿出足够出库数量的托盘计算
                        //更新出库单状态
                        BillOutMaster billOutMaster = inserData.getBillOutMaster();
                        billOutMaster.setState(BillState.BILLOUT_STATE_TWO);//任务已下发的状态
                        int editBillOut= billOutMasterService.updateBillOutMaster(billOutMaster);
                        //同步任务表
                        String createUserName = ShiroUtils.getLoginName();
                        taskInfo.setCreateUserName(createUserName);
                        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
                        taskInfo.setPid(sysUser.getPid());//添加班次
                        taskInfo.setType(TaskInfoState.BILLOUT_TYPE);//任务类型
                        taskInfo.setState(TaskInfoState.STATE_ZERO);//状态
                        taskInfo.setBillId(billOutMaster.getBillId());
                        taskInfo.setBoxCode(boxItemDto.getBoxCode());//托盘编号
                        taskInfo.setBillOutDetailId(billOutDetail.getBillOutDetailId());//明细ID
                        long taskNumber = taskInfoService.generateTaskNumber();//设置任务号
                        taskInfo.setTaskNumber(taskNumber);
                        taskInfo.setQuantity(billOutDetail.getQuantity());//数量
                        taskInfo.setRoadWay(boxItemDto.getShelfId());//巷道
                        taskInfo.setCellCode(boxItemDto.getCellCode());//货位编码
                        taskInfo.setCellId(boxItemDto.getCellId());//货位ID
                        int saveTask=taskInfoService.insertTaskInfo(taskInfo);//同步信息到任务表，任务表与队列消息一致
                        taskInfoList.add(taskInfo);
                        BoxInfo boxInfo = new BoxInfo();
                        boxInfo.setBoxCode(boxItemDto.getBoxCode());
                        boxInfo.setBoxState(BoxInfoState.BOXINFO_STATE_TWO);
                        boxInfo.setHasGoods(BoxInfoState.BOXINFO_GOODS_ONT);
                        int editBoxInfo=boxInfoService.updateBoxInfo(boxInfo);
                        CellInfo cellInfo = new CellInfo();
                        cellInfo.setState(CellInfoState.STATE_TWO);//出库中的状态
                        cellInfo.setCellId(taskInfo.getCellId());
                        int editCell=cellInfoService.updateCellInfo(cellInfo);
                        if(editBillOut==0||saveTask==0||editBoxInfo==0||editCell==0){
                            try {
                                log.error("修改出库单状态："+editBillOut+"下发任务状态："+saveTask+"修改托盘状态："+editBoxInfo+"修改货位状态："+editCell);
                                throw new Exception("出库下发任务出错，手动抛异常");
                            } catch (Exception e) {
                                e.printStackTrace();
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            }
                            if(editBillOut==0){
                                return ResultGenerator.genFailResult(CommonCode.EDITBILLOUT_ERROR, "false");
                            }else if(saveTask==0){
                                return ResultGenerator.genFailResult(CommonCode.TASKINFO_OUTERROR, "false");
                            }else if(editBoxInfo==0){
                                return ResultGenerator.genFailResult(CommonCode.EDITBOXINFO_ERROR, "false");
                            }else if(editCell==0){
                                return ResultGenerator.genFailResult(CommonCode.EDITCELLINFO_ERROR, "false");
                            }
                        }

                        BoxItemDto boxItemDtoOut = new BoxItemDto();
                        boxItemDtoOut.setBoxCode(boxItemDto.getBoxCode());
                        boxItemDtoOut.setBillOutDetailId(billOutDetail.getBillOutDetailId());
                        BoxItemDto boxItemDto1 = boxItemService.selectBoxItemByboxItemDto(boxItemDtoOut);
                        BoxItemOutList.add(boxItemDto1);//放入要发送的消息内容
                        if (sumQuantity >= billOutDetail.getQuantity()) {
                            Integer billOutDetailId = billOutDetail.getBillOutDetailId();
                            BillOutDetail bybillOutDetailId = billOutDetailService.findBybillOutDetailId(billOutDetailId);
                            billOutDetail.setQuantityDelivery(sumQuantity+bybillOutDetailId.getQuantityDelivery());//出库实际数量
                            billOutDetail.setSendBack(sumQuantity - billOutDetail.getQuantity()+bybillOutDetailId.getSendBack());//比实际多余的数量
                            //将多余的料分配进暂存区
                            if (sumQuantity > billOutDetail.getQuantity()) {
                                AreaBoxItem areaBoxItem = new AreaBoxItem();
                                areaBoxItem.setItemCode(boxItemDto.getItemCode());
                                areaBoxItem.setBatch(boxItemDto.getBatch());
                                areaBoxItem.setQuantity(sumQuantity - billOutDetail.getQuantity());
                                areaBoxItem.setBillOutDtailId(billOutDetail.getBillOutDetailId());
                                areaBoxItem.setPd(boxItemDto.getPd());
                                areaBoxItem.setExp(boxItemDto.getExp());
                                areaBoxItemService.saveAreaBoxItem(areaBoxItem);
                            }
                            // areaBoxItemHistoryService.saveAreaBoxItem(areaBoxItem);
                            billOutDetailService.updateBillOutMasterQuantityDelivery(billOutDetail);//更新出库明细
                            break;
                        }
                    }
                    List<Integer> collegeOrder = Arrays.asList(2, 4, 3, 1);//对排进行排序
                    List<Integer> addressOrder = Arrays.asList(4, 3, 2, 1);//对层进行排序
                    Collections.sort(BoxItemOutList, new Comparator<BoxItemDto>() {
                        public int compare(BoxItemDto boxItemDto1, BoxItemDto boxItemDto2) {
                            if (boxItemDto1.getTeamPai().equals(boxItemDto2.getTeamPai())) {
                                int io1 = addressOrder.indexOf(boxItemDto1.getsFloor());
                                int io2 = addressOrder.indexOf(boxItemDto2.getsFloor());
                                return io1 - io2;
                            } else {
                                int io1 = collegeOrder.indexOf(boxItemDto1.getTeamPai());
                                int io2 = collegeOrder.indexOf(boxItemDto2.getTeamPai());
                                return io1 - io2;
                            }
                        }
                    });

                    for (BoxItemDto boxItemDto : BoxItemOutList) {
                        //List<BoxItemDto> boxCodeList = new ArrayList<>();//队列放入的托盘详细信息集合2.0
                        Integer roadWay = boxItemDto.getRoadWay();
                       // boxCodeList.add(boxItemDto);
                        String topic = null;
                        if (roadWay == 76) {
                            topic = TopicName.TOPIC_ONE;
                            outMq(boxItemDto, taskInfoList, topic);
                        } else if (roadWay == 77) {
                            topic = TopicName.TOPIC_TWO;
                            outMq(boxItemDto, taskInfoList, topic);
                        } else if (roadWay == 79) {
                            topic = TopicName.TOPIC_THREE;
                            outMq(boxItemDto, taskInfoList, topic);
                        }
                    }
                }else {
                    return ResultGenerator.genFailResult(CommonCode.STOCK_NOT_ENOUGH, "false");
                }
            }
            return ResultGenerator.genSuccessResult();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
        }
        return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
    }





    */
/**
     * 异常补货
     *
     * @param
     * @return
     *//*

    @Transactional(rollbackFor = Exception.class)
    public Boolean BillOutError(Long number) {
        try {
            Integer sumQuantity = 0;//初始化总数
            List<BoxItemDto> BoxItemOutList = new ArrayList<>();//队列放入的托盘详细信息集合
            List<TaskInfo> taskInfoList = new ArrayList<>();//对列放入的任务集合
            TaskInfo info = taskInfoService.selectTaskInfoByTaskNumber(number);
            Integer billOutDetailId = info.getBillOutDetailId();
            BillOutDetail billOutDetail = billOutDetailService.findBybillOutDetailId(billOutDetailId);

            Integer sendBack = billOutDetail.getSendBack();//该明细放到暂存区数量
            Integer quantityDelivery = billOutDetail.getQuantityDelivery();//任务总数
            Integer taskQuantity = info.getQuantity();//该任务出库数量
            Integer billOutDetailQuantity = billOutDetail.getQuantity();//订单总数
            Integer quantity = 0;//下面任务出库的数量

            if(quantityDelivery-taskQuantity<billOutDetailQuantity){
                AreaBoxItemDto areaBoxItemDto = new AreaBoxItemDto();
                areaBoxItemDto.setItemCode( billOutDetail.getItemCode());
                areaBoxItemDto.setBillOutDtailId(billOutDetailId);
                quantity =quantityDelivery-taskQuantity;//出库单个物料的总数量
                this.editAreaBoxItem(sendBack,areaBoxItemDto);
                BoxItem boxItem = new BoxItem();
                if(null!=billOutDetail.getItemCode()){
                    boxItem.setItemCode(billOutDetail.getItemCode());
                }
                if(null!=billOutDetail.getBatch()){
                    boxItem.setBatch(billOutDetail.getBatch());
                }
                if(null!=billOutDetail.getStartTime() && null!=billOutDetail.getEndTime()){
                    boxItem.setStartTime(billOutDetail.getStartTime());
                    boxItem.setEndTime(billOutDetail.getEndTime());
                }
                Integer sumQuantityByItemCode = boxItemService.getSumQuantityByItemCode(boxItem);//获取物料的总库存数
                //判断不够出库就返回false
                if (sumQuantityByItemCode < billOutDetail.getQuantity()) {
                    logger.error("出库异常补货时，库存不足,任务号："+number);
                    return false;
                }
                String itemCode = billOutDetail.getItemCode();//出库的单个物料
                if(quantity>0){
                    BoxItemDto brequest = new BoxItemDto();
                    brequest.setItemCode(itemCode);
                    List<BoxItemDto> listByItemCode = boxItemService.findListByItemCode(brequest);
                    if (listByItemCode.size()>0){
                        for (BoxItemDto boxItemDto : listByItemCode) {
                            //判断是否是需盘点
                            if (boxItemDto.getTeamPai() % 2 != 0) {
                                Integer cellId = boxItemDto.getCellId();
                                Integer sPai = boxItemDto.getsPai();//排
                                Integer sLie = boxItemDto.getsLie();//列
                                Integer sFloor = boxItemDto.getsFloor();//层
                                Integer teamPai = boxItemDto.getTeamPai();//分划排
                                if(sPai% 2 != 0){
                                    sPai=sPai+1;
                                }else{
                                    sPai=sPai-1;
                                }
                                teamPai=teamPai+1;

                                CellInfo cellInfo = new CellInfo();
                                cellInfo.setsPai(sPai);
                                cellInfo.setsLie(sLie);
                                cellInfo.setsFloor(sFloor);
                                cellInfo.setTeamPai(teamPai);

                                CellInfo stateByCellInfo = cellInfoService.findStateByCellInfo(cellInfo);
                                if (stateByCellInfo.getState() == 6) {
                                    continue;
                                }
                            }
                            TaskInfo taskInfo = new TaskInfo();//任务对象
                            sumQuantity = boxItemDto.getQuantity() + sumQuantity;//当前托盘上物品的数量，累加，拿出足够出库数量的托盘   计算
                            taskInfo.setType(TaskInfoState.BILLOUT_TYPE);//任务类型
                            taskInfo.setState(TaskInfoState.STATE_ZERO);//状态
                            taskInfo.setBillId(billOutDetail.getBillId());
                            taskInfo.setBoxCode(boxItemDto.getBoxCode());//托盘编号
                            taskInfo.setBillOutDetailId(billOutDetail.getBillOutDetailId());//明细ID
                            long taskNumber = taskInfoService.generateTaskNumber();//设置任务号
                            taskInfo.setTaskNumber(taskNumber);
                            taskInfo.setQuantity(boxItemDto.getQuantity());//数量
                            taskInfo.setRoadWay(boxItemDto.getShelfId());//巷道
                            taskInfo.setCellCode(boxItemDto.getCellCode());//货位编码
                            taskInfo.setCellId(boxItemDto.getCellId());//货位ID
                            taskInfo.setStowCreateTime(boxItemDto.getStowCreateTime());//入库时，托盘码垛的时间
                            int saveTaskResult=taskInfoService.insertTaskInfo(taskInfo);//同步信息到任务表，任务表与队列消息一致
                            taskInfoList.add(taskInfo);
                            BoxInfo boxInfo = new BoxInfo();
                            boxInfo.setBoxCode(boxItemDto.getBoxCode());
                            boxInfo.setBoxState(BoxInfoState.BOXINFO_STATE_TWO);
                            boxInfo.setHasGoods(BoxInfoState.BOXINFO_GOODS_ONT);
                            int editBoxResult=boxInfoService.updateBoxInfo(boxInfo);
                            CellInfo cellInfo = new CellInfo();
                            cellInfo.setState(CellInfoState.STATE_TWO);//出库中的状态
                            cellInfo.setCellId(taskInfo.getCellId());
                            int editCellResult=cellInfoService.updateCellInfo(cellInfo);
                            BoxItemDto boxItemDtoOut = new BoxItemDto();
                            boxItemDtoOut.setBoxCode(boxItemDto.getBoxCode());
                            boxItemDtoOut.setBillOutDetailId(billOutDetail.getBillOutDetailId());
                            //BoxItemDto boxItemDto1 = boxItemService.selectBoxItemByBoxCode(boxItemDto.getBoxCode());
                            BoxItemDto boxItemDto1 = boxItemService.selectBoxItemByboxItemDto(boxItemDtoOut);
                            BoxItemOutList.add(boxItemDto1);//放入要发送的消息内容
                            if (sumQuantity >= quantity) {
                                BillOutDetail bybillOutDetailId = billOutDetailService.findBybillOutDetailId(billOutDetailId);
                                billOutDetail.setQuantityDelivery(sumQuantity+bybillOutDetailId.getQuantityDelivery());//出库实际数量
                                billOutDetail.setSendBack(sumQuantity - quantity+bybillOutDetailId.getSendBack());//比实际多
                                //将多余的料分配进暂存区
                                if (sumQuantity >quantity) {
                                    AreaBoxItem areaBoxItem = new AreaBoxItem();
                                    areaBoxItem.setItemCode(boxItemDto.getItemCode());
                                    areaBoxItem.setBatch(boxItemDto.getBatch());
                                    areaBoxItem.setQuantity(sumQuantity - quantity);
                                    areaBoxItem.setBillOutDtailId(billOutDetail.getBillOutDetailId());
                                    areaBoxItem.setPd(boxItemDto.getPd());
                                    areaBoxItem.setExp(boxItemDto.getExp());
                                    int i = areaBoxItemService.saveAreaBoxItem(areaBoxItem);
                                    if(i==0){
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                    }
                                }
                                // areaBoxItemHistoryService.saveAreaBoxItem(areaBoxItem);
                                billOutDetail.setQuantityDelivery(billOutDetail.getQuantityDelivery()-info.getQuantity());//出库实际数量
                                int editBillOutResult=billOutDetailService.updateBillOutMasterQuantityDelivery(billOutDetail);//更新出库明细
                                int saveTaskHistoryResult= taskInfoHistoryService.saveTaskInfoHistory(taskInfoService.selectTaskInfoById(info.getId()));//任务完成同步到历史表
                                int deTaskResult =taskInfoService.deleteTaskInfoById(info.getId());
                                if(saveTaskResult==0||editBoxResult==0||editCellResult==0||editBillOutResult==0||saveTaskHistoryResult==0||deTaskResult==0){
                                    try {
                                       // log.error("修改出库单状态："+editBillOut+"下发任务状态："+saveTask+"修改托盘状态："+editBoxInfo+"修改货位状态："+editCell);
                                        throw new Exception("出库下发任务出错，手动抛异常");
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                    }
                                }
                            }
                        }
                        //拿出货位信息，判断是否是需盘点；是，能continue;   如果当前托盘在组内的奇数排，偶数排的货需盘点，则continue
                        //分 奇偶 两组  偶数优先
                        //按照组内的2413，先高再低的原则，分配已经找到满足条件的托盘顺序
                        //BoxItemOutList.sort(comparing(BoxItemDto::getTeamPai));
                        List<Integer> collegeOrder = Arrays.asList(2,4,3,1);//对排进行排序
                        List<Integer> addressOrder = Arrays.asList(4,3,2,1);//对层进行排序
                        Collections.sort(BoxItemOutList, new Comparator<BoxItemDto>()
                        {
                            public int compare(BoxItemDto boxItemDto1, BoxItemDto boxItemDto2)
                            {
                                if(boxItemDto1.getTeamPai().equals(boxItemDto2.getTeamPai())){
                                    int io1 = addressOrder.indexOf(boxItemDto1.getsFloor());
                                    int io2 = addressOrder.indexOf(boxItemDto2.getsFloor());
                                    return io1 - io2;
                                }else{
                                    int io1 = collegeOrder.indexOf(boxItemDto1.getTeamPai());
                                    int io2 = collegeOrder.indexOf(boxItemDto2.getTeamPai());
                                    return io1 - io2;
                                }
                            }
                        });
                        //排序结束
                        for(BoxItemDto boxItemDto1:BoxItemOutList){
                            Integer roadWay = boxItemDto1.getRoadWay();
                            //boxCodeList.add(boxItemDto1);
                            String topic=null;
                            if(roadWay==76){
                                topic=TopicName.TOPIC_ONE;
                                outMq(boxItemDto1,taskInfoList,topic);
                            }else if (roadWay==77){
                                topic=TopicName.TOPIC_TWO;
                                outMq(boxItemDto1,taskInfoList,topic);
                            }else if (roadWay==79){
                                topic=TopicName.TOPIC_THREE;
                                outMq(boxItemDto1,taskInfoList,topic);
                            }
                        }
                        return true;
                    }else {
                        logger.error("出库异常补货时,库存不够，不足以出库");
                    }
                }else {
                    int re1= taskInfoHistoryService.saveTaskInfoHistory(taskInfoService.selectTaskInfoById(info.getId()));//任务完成同步到历史表
                    int re2 =taskInfoService.deleteTaskInfoById(info.getId());
                    if(re1==0&&re2==0){
                        logger.error("不用下发新的任务时。删除任务失败。");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
                }
            }else if(quantityDelivery-taskQuantity==billOutDetailQuantity){
                AreaBoxItemDto areaBoxItemDto = new AreaBoxItemDto();
                areaBoxItemDto.setItemCode( billOutDetail.getItemCode());
                areaBoxItemDto.setBillOutDtailId(billOutDetailId);
                this.editAreaBoxItem(sendBack,areaBoxItemDto);
                billOutDetail.setQuantityDelivery(billOutDetailQuantity);//出库实际数量
                billOutDetail.setSendBack(0);//比实际多余的数量
                billOutDetailService.updateBillOutMasterQuantityDelivery(billOutDetail);
            }else if(quantityDelivery-taskQuantity>billOutDetailQuantity){
                    AreaBoxItemDto areaBoxItemDto = new AreaBoxItemDto();
                    areaBoxItemDto.setItemCode( billOutDetail.getItemCode());
                    areaBoxItemDto.setBillOutDtailId(billOutDetailId);
                    sendBack=quantityDelivery-billOutDetailQuantity-sendBack;
                    this.editAreaBoxItem(sendBack,areaBoxItemDto);
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
        }
        return false;
    }


    public void editAreaBoxItem( Integer sendBack, AreaBoxItemDto areaBoxItemDto){
        if (sendBack>0){
            List<AreaBoxItemDto> areaBoxItemList  = areaBoxItemService.findAreaBoxItemList(areaBoxItemDto);
            for (AreaBoxItemDto boxItemDto : areaBoxItemList) {
                    if(sendBack==boxItemDto.getQuantity()){
                        areaBoxItemService.delectAreaBoxItembyId(boxItemDto.getId());
                        break;
                    }else if(sendBack<boxItemDto.getQuantity()){
                        AreaBoxItem areaBoxItem = new AreaBoxItem();
                        areaBoxItem.setId(boxItemDto.getId());
                        areaBoxItem.setQuantity(boxItemDto.getQuantity()-sendBack);
                        areaBoxItemService.editAreaBoxItem(areaBoxItem);//修改暂存区物料的数量
                        break;
                    }else {
                        areaBoxItemService.delectAreaBoxItembyId(boxItemDto.getId());
                    }

            }
        }
    }


    */
/**
     * 手持机重新下发
     * @param
     * @return
     *//*

    @RequestMapping(value = "/taskInfoAgainIssued", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public Result taskInfoAgainIssued(@RequestBody TaskInfo taskInfo) {
        try {
            Integer id = taskInfo.getId();
            TaskInfo taskInfo1 = taskInfoService.selectTaskInfoById(id);
            BoxItemDto boxItemDto1 = boxItemService.selectBoxItemByBoxCode(taskInfo1.getBoxCode());
            String body = JSONArray.toJSONString(boxItemDto1);
            SendResult stations = producer.send("getTopic", "pull", body);//放入队列,返回消息状态
            //同步rocket表
            Rocket rocket = new Rocket();
            rocket.setMessageId(stations.getMsgId());
            rocket.setBody(body);
            rocketService.saveRocket(rocket);
            return ResultGenerator.genSuccessResult();
            // TaskInfo taskInfo = new TaskInfo();

        } catch (InterruptedException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "false");
    }

}
*/
